<template>
	<view>
		<view class="form">
			<view class="form_item">
				<view class="label">
					商品名称
				</view>
				<view class="input">
					<u--input placeholder="请输入商品名称" placeholderStyle='color:#8B8B8B;' border='none' v-model="form.name">
					</u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					出售价格
				</view>
				<view class="input">
					<u--input placeholder="请输入实际出售价格" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.price"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					市场价格
				</view>
				<view class="input">
					<u--input placeholder="请输入市场出售价格" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.oldprice"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					发货地址
				</view>
				<view class="input">
					<u--input placeholder="请输入发货地址" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.send_address"></u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<view class="form_item">
				<view class="label">
					产品规格
				</view>
				<view class="input" @click="clickto('/pages/product/specifications')">
					<u--input readonly placeholder="请添加产品规格图片" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="form.card"></u--input>
				</view>
				<image @click="clickto('/pages/product/specifications')" class="right_icon_jia"
					src="/static/img/product/jia.png" mode=""></image>
			</view>
			<scroll-view scroll-x="true" style="width: 689rpx;" v-if="gglist.length>0">
				<view class="pic_box" style="flex-wrap: nowrap;">
					<view class="ggitem" v-for="(item,index) in gglist" :key="index">
						<image class="ggpic" :src="item.image" mode=""
							@click="clickto('/pages/product/specifications?id='+item.id)"></image>
						<view class="ggname">
							{{item.text}}
						</view>
						<view class="delbox" @click="delspec(index)">
							<image class="del_icon" src="/static/img/business/picdel.png" mode=""></image>
						</view>
					</view>
				</view>
			</scroll-view>

			<view class="form_item">
				<view class="label">
					商品分类
				</view>
				<view class="input" @click="clickpick">
					<u--input readonly placeholder="请添加商品分类" placeholderStyle='color:#8B8B8B;' border='none'
						v-model="product_category"></u--input>
					<!-- <u-picker  ref="uPicker"  @change="changeHandler" :show="show" border='none' :columns="category" keyName="name"></u-picker> -->
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view>
			<!-- <view class="form_item">
				<view class="label">
					库存数量
				</view>
				<view class="input">
					<u--input placeholder="请输入库存数量" placeholderStyle='color:#8B8B8B;' border='none' v-model="form.num">
					</u--input>
				</view>
				<image class="right_icon" src="/static/img/my/right.png" mode=""></image>
			</view> -->
			<view class="form_title">
				<view class="title_name">
					添加展示图片<text style="font-weight: normal;font-size: 24rpx;color: #9B9B9B;">(可多张）</text>
				</view>
			</view>
			<view class="pic_box">
				<view class="pic_item" v-for="(item,index) in zsimgs" :key="index">
					<image class="pic_bg" @click="imgPreview(zsimgs,index)" :src="baseurl+item" mode=""></image>
					<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);"
						@click="resup(index,'zs')">
						重新上传
					</view>
					<view class="delbox" @click="del(index,'zs')">
						<image class="del_icon" src="/static/img/business/picdel.png" mode=""></image>
					</view>
				</view>
				<view class="pic_item" @click="up('zs')">
					<image class="pic_bg" src="/static/img/my/picbg.png" mode=""></image>
					<view class="pic_item_name">
						点击上传
					</view>
				</view>

			</view>
			<view class="form_title">
				<view class="title_name">
					产品详情图
				</view>
			</view>
			<view class="pic_box">
				<view class="pic_item" v-for="(item,index) in xqimgs" :key="index">
					<image class="pic_bg" @click="imgPreview(xqimgs,index)" :src="baseurl+item" mode=""></image>
					<view class="pic_item_name" style="background-color: rgba(0, 0, 0, 0.5);"
						@click="resup(index,'xq')">
						重新上传
					</view>
					<view class="delbox" @click="del(index,'xq')">
						<image class="del_icon" src="/static/img/business/picdel.png" mode=""></image>
					</view>
				</view>
				<view class="pic_item" v-if="!xqimgs.length>0" @click="up('xq')">
					<image class="pic_bg" src="/static/img/my/picbg.png" mode=""></image>
					<view class="pic_item_name">
						点击上传
					</view>
				</view>
			</view>
			<view class="btn" @click="submit">
				确认发布
			</view>
		</view>
		<u-picker :show="show" @cancel='show=false' :defaultIndex='defaultIndex' :columns="product_category_list"
			keyName='name' @confirm='confirm'></u-picker>
	</view>
</template>

<script>
	import {
		getCateGory,
		addProduct,
		productById,
		editProduct,
		getgoodsSpec
	} from '@/api/product.js'
	export default {
		data() {
			return {
				show: false,
				form: {
					name: '',
					intro: '',
					images: '',
					price: '',
					product_category_id: "",
					root_id: "",
					description: "",
					return_score: "",
					store: "",
					spec_ids: "",
					send_address:'',
					type:1
				},
				id: "",
				zsimgs: [],
				xqimgs: [],
				product_category_list: [],
				product_category: '',
				gglist: [],
				defaultIndex: [],
				baseurl: '',
				baseggl: []
			};
		},
		methods: {
			clickto(url) {
				uni.$u.route(url)
			},
			async getCateGorys() {
				// this.product_category_list=[]
				let res = await getCateGory()
				this.product_category_list.push(res.data.data)
				if (this.id) {
					this.defaultIndex = []
					this.product_category_list[0].map((t, i) => {
						if (t.id == this.form.product_category_id) {
							this.defaultIndex.push(i)
							this.product_category = t.name
						}
					})
				}
				// console.log(this.product_category_list)

			},
			async productByIds(id) {
				let res = await productById(id);
				this.form = res.data.data
				if (this.form.images) {
					let imgs = this.form.images.split(',')
					this.zsimgs = imgs
				}
				if (this.form.description) {
					let xqimgs1 = this.form.description.split(',')
					this.xqimgs = xqimgs1
				}
				this.product_category_list = []
				
				this.gglist = this.form.spec
				this.$store.commit('setspec', this.gglist)
				// console.log(this.gglist)
				this.getCateGorys()
			},
			submit() {
				this.form.images = this.zsimgs.join(',')
				this.form.description = this.xqimgs.join(',')
				this.form.id = this.id
				// this.form.spec_ids
				let spec=[]
				this.gglist.map(t=>{
					spec.push(t.id)
				})
				console.log(spec)
				this.form.spec_ids=spec.toString()
				// 检验
				let {
					name,
					intro,
					images,
					price,
					product_category_id,
					root_id,
					description,
					return_score,
					store,
					spec_ids,
					id,
					oldprice,
					send_address,
					type
				} = this.form
				// 名称
				if (!price) {
					uni.showToast({
						title: '请输入出售价格',
						icon: 'none'
					})
					return
				}
				if (!oldprice) {
					uni.showToast({
						title: '请输入市场价格',
						icon: 'none'
					})
					return
				}
				if (!spec_ids) {
					uni.showToast({
						title: '请添加产品规格',
						icon: 'none'
					})
					return
				}
				
				// send_address
				if (!send_address) {
					uni.showToast({
						title: '请输入发货地址',
						icon: 'none'
					})
					return
				}
				if (!product_category_id) {
					uni.showToast({
						title: '请选择商品分类',
						icon: 'none'
					})
					return
				}
				if (!images) {
					uni.showToast({
						title: '请添加展示图片',
						icon: 'none'
					})
					return
				}
				if (!description) {
					uni.showToast({
						title: '请添加详情图片',
						icon: 'none'
					})
					return
				}
				if (this.form.id) {
					// 修改
					editProduct({
						name,
						intro,
						images,
						price,
						product_category_id,
						root_id,
						description,
						return_score,
						store,
						spec_ids,
						send_address,
						oldprice,
						id,
						type
					}).then(res => {
						if (res.data.code == 1) {
							this.$store.commit('setspec',[])
							uni.showToast({
								title:'修改成功'
							})
							uni.navigateBack()
							this.$store.commit('setspec',[])
						}
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					})
				} else {
					addProduct({
						name,
						intro,
						images,
						price,
						product_category_id,
						root_id,
						description,
						return_score,
						store,
						spec_ids,
						oldprice,
						send_address,
						type
					}).then(res => {
						if (res.data.code == 1) {
							uni.showToast({
								title:'添加成功'
							})
							uni.navigateBack()
							this.$store.commit('setspec',[])
						}
						uni.showToast({
							title: res.data.msg,
							icon: 'none'
						})
					})
				}
			},
			confirm(e) {
				this.show = false
				this.form.product_category_id = e.value[0].id
				this.product_category = e.value[0].name
			},
			clickpick() {
				this.show = true
			},
			// 删除规格
			delspec(i) {
				this.gglist.splice(i,1)
				this.$store.commit('setspec',this.gglist)
			},
			// 上传图片
			async up(type) {
				let _this = this
				let count = 0
				if (type == 'zs') {
					count = 9
				}
				if (type == 'xq') {
					count = 1
				}
				await uni.chooseImage({
					count: count,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						tempFilePaths.map(t => {
							uni.showLoading({
								title: '上传中'
							})
							uni.uploadFile({
								url: _this.$BASE_URL +
									'/api/common/upload', //仅为示例，非真实的接口地址
								filePath: t,
								name: 'file',
								header: {
									token: uni.getStorageSync('token')
								},
								success: (uploadFileRes) => {
									if (type == 'zs') {
										_this.zsimgs.push(JSON.parse(uploadFileRes
												.data)
											.data.url)
									}
									if (type == 'xq') {
										_this.xqimgs.push(JSON.parse(uploadFileRes
												.data)
											.data.url)
									}
									uni.hideLoading()
									uni.showToast({
										title: JSON.parse(uploadFileRes.data)
											.msg,
										icon: 'none'
									})
								},

							});
						})
					}
				});
			},
			imgPreview(urls, i) {
				console.log(urls)
				let us = []
				urls.map(t => {
					us.push(this.baseurl + t)
				})
				uni.previewImage({
					urls: us,
					current: i
				});
			},
			// 重新上传
			async resup(i, type) {
				let _this = this
				await uni.chooseImage({
					count: 1,
					success: (chooseImageRes) => {
						const tempFilePaths = chooseImageRes.tempFilePaths;
						// console.log(tempFilePaths)
						uni.showLoading({
							title: '上传中'
						})
						uni.uploadFile({
							url: _this.$BASE_URL +
								'/api/common/upload', //仅为示例，非真实的接口地址
							filePath: tempFilePaths[0],
							name: 'file',
							header: {
								token: uni.getStorageSync('token')
							},
							success: (uploadFileRes) => {
								// _this.zsimgs[i]=(JSON.parse(uploadFileRes.data)
								// 	.data.fullurl)
								// 	// console.log(uploadFileRes)
								uni.hideLoading()
								if (type == 'zs') {
									_this.$set(_this.zsimgs, i, JSON.parse(uploadFileRes.data)
										.data
										.url)
								}
								if (type == 'xq') {
									_this.$set(_this.xqimgs, i, JSON.parse(uploadFileRes.data)
										.data
										.url)
								}

								uni.showToast({
									title: JSON.parse(uploadFileRes.data).msg,
									icon: 'none'
								})
							},
						});
					}
				});
			},
			// 删除图片
			del(i, type) {
				console.log(i, type)
				if (type == 'zs') {
					this.zsimgs.splice(i, 1)
				}
				if (type == 'xq') {
					this.xqimgs.splice(i, 1)
				}
			},
			re() {
				if (this.id) {
					this.gglist = this.$store.state.spec
					this.productByIds({
						id: this.id
					})
				}

			}
		},
		onLoad(item) {
			// console.log(2)
			this.baseurl = this.$BASE_URL
			this.form.type=item.type
			this.getCateGorys()
			if (item.id) {
				this.id = item.id
				this.productByIds({
					id: this.id
				})
			}
		},
		onShow() {
			// console.log(this.$store.state.spec)
			this.$set(this, 'gglist', this.$store.state.spec)
		}
	}
</script>

<style lang="less">
	.form {
		padding: 10rpx 31rpx 70rpx;
		border-top: 2rpx solid #F4F6F8;

		.form_item {
			display: flex;
			align-items: center;
			justify-content: space-between;
			border-bottom: 1rpx solid #F4F6F8;
			height: 105rpx;

			.label {
				color: #333;
				font-size: 30rpx;
				font-weight: bold;
				padding-left: 10rpx;
				width: 210rpx;
			}

			.input {
				font-size: 30rpx;
				flex: 1;
			}

			.right_icon {
				width: 15rpx;
				height: 24rpx;
			}

			.right_icon_jia {
				width: 34rpx;
				height: 34rpx;
			}
		}

		.form_title {
			display: flex;
			padding: 45rpx 0 36rpx 10rpx;
			align-items: center;
			color: #333;
			font-size: 30rpx;
			font-weight: bold;

			.form_line {
				width: 8rpx;
				height: 28rpx;
				background: #FE712A;
				border-radius: 4rpx;
				margin-right: 10rpx;
			}
		}
	}

	.pic_box {
		display: flex;
		flex-wrap: wrap;

		.pic_item {
			width: 198rpx;
			height: 198rpx;
			position: relative;
			margin-right: 30rpx;
			margin-bottom: 20rpx;
			border-radius: 10rpx;
			overflow: hidden;
		}

		.pic_bg {
			width: 198rpx;
			height: 198rpx;
			position: absolute;
			border-radius: 10rpx;
			top: 0;
			left: 0;
		}

		.pic_item_name {
			text-align: center;
			width: 100%;
			position: absolute;
			bottom: 0;
			color: #fff;
			font-size: 26rpx;
			line-height: 60rpx;
		}
	}

	.btn {
		width: 687rpx;
		line-height: 114rpx;
		background: linear-gradient(153deg, #FF8717, #FF5F00);
		border-radius: 57rpx;
		text-align: center;
		font-size: 36rpx;
		color: #fff;
		margin-top: 77rpx;
	}

	.delbox {
		width: 29rpx;
		height: 25rpx;
		background: rgba(0, 0, 0, .5);
		border-radius: 7rpx 0 0 12rpx;

		display: flex;
		justify-content: center;
		align-items: center;
		position: absolute;
		top: 0;
		right: 0;
	}

	.del_icon {
		width: 12rpx;
		height: 12rpx;
	}

	.ggitem {
		border-radius: 10rpx;
		margin-right: 15rpx;
		position: relative;

		.ggpic {
			width: 219rpx;
			height: 208rpx;
			border-radius: 10rpx 10rpx 0 0;
		}

		.ggname {
			background-color: #F5F5F5;
			line-height: 56rpx;
			text-align: center;
			font-size: 22rpx;
			color: #333;
			margin-top: -3px;
			border-radius: 0px 0px 10rpx 10rpx;
		}
	}
</style>
